#pragma once
#include <pthread.h>
#include <sched.h>
#include <iostream>

namespace CpuAffinity
{
    // 将当前线程绑定到指定CPU核
    inline void bindThreadToCpu(int cpu_id)
    {
        cpu_set_t cpuset;
        CPU_ZERO(&cpuset);
        CPU_SET(cpu_id, &cpuset);

        pthread_t current_thread = pthread_self();
        int rc = pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
        if (rc != 0)
        {
            std::cerr << "[CpuAffinity] Failed to bind thread to CPU " << cpu_id << std::endl;
        }
        else
        {
            std::cout << "[CpuAffinity] Thread bound to CPU " << cpu_id << std::endl;
        }
    }

}
